﻿Public Class Destudio

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of Eestudio)
        Dim estudios As List(Of Eestudio) = Nothing 'Es una coleccion de lista tipado (vendedor)

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_estudio_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            estudios = New List(Of Eestudio)
                            Dim estudio As Eestudio = Nothing 'tipo 
                            Do While dr.Read
                                estudio = New Eestudio( _
                                    dr.GetInt32(dr.GetOrdinal("idEstudio")), _
                                    dr.GetString(dr.GetOrdinal("descEstudio")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                estudios.Add(estudio)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return estudios
    End Function
    Public Shared Function SeleccionarTodoXestado(ByVal descEmpresa As String) As List(Of Eestudio)
        Dim estudios As List(Of Eestudio) = Nothing 'Es una coleccion de lista tipado (vendedor)

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_estudio_selxEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            estudios = New List(Of Eestudio)
                            Dim estudio As Eestudio = Nothing 'tipo 
                            Do While dr.Read
                                estudio = New Eestudio( _
                                    dr.GetInt32(dr.GetOrdinal("idEstudio")), _
                                    dr.GetString(dr.GetOrdinal("descEstudio")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                estudios.Add(estudio)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return estudios
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal estudio As Eestudio) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_estudio_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@descEstudio", SqlDbType.VarChar, 55).Value = estudio.descEstudio
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = estudio.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal estudio As Eestudio) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_estudio_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idEstudio", SqlDbType.Int).Value = estudio.idEstudio
                cmd.Parameters.Add("@descEstudio", SqlDbType.VarChar, 55).Value = estudio.descEstudio
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = estudio.estado
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal estudio As Eestudio) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_estudio_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idEstudio", SqlDbType.Int).Value = estudio.idEstudio
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = estudio.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
